package calorie.com.calorie.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import com.google.gson.Gson;
import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.event.BusFactory;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.ActivationUserContact;
import calorie.com.calorie.eventbus.EventImpl;
import calorie.com.calorie.response.ResultDTO;
import calorie.com.calorie.response.User;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;

/**
 * 激活帐户
 */
public class ActiveAccountActivity extends MvpActivity<ActivationUserContact.ActivationUserPresent> implements ActivationUserContact.IActivationUserView {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_identity_number)
    EditText etIdentityNumber;
    @BindView(R.id.et_email_address)
    EditText etEmailAddress;
    @BindView(R.id.et_height)
    EditText etHeight;
    @BindView(R.id.et_weight)
    EditText etWeight;
    @BindView(R.id.et_receive_address)
    EditText etReceiveAddress;
    @BindView(R.id.return_btn)
    ImageButton returnBtn;

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_active_account;
    }


    @OnClick(R.id.btn_confirm)
    public void onViewClicked(View view) {
        String realName = etName.getText().toString().trim();
        String identityNum = etIdentityNumber.getText().toString().trim();
        String email = etEmailAddress.getText().toString().trim();
        String weight = etWeight.getText().toString();
        String height = etHeight.getText().toString().trim();
        String receivingAddress = etReceiveAddress.getText().toString().trim();
        Integer userId = StaticValue.user.getId();
        if (ObjectUtils.isEmpty(realName)) {
            ToastUtils.showLongToast(context, "请输入姓名");
            return;
        }
        if (ObjectUtils.isEmpty(identityNum)) {
            ToastUtils.showLongToast(context, "请输入身份证号");
            return;
        }
        if (ObjectUtils.isEmpty(email)) {
            ToastUtils.showLongToast(context, "请输入邮箱");
            return;
        }
        if (ObjectUtils.isEmpty(weight)) {
            ToastUtils.showLongToast(context, "请输入体重");
            return;
        }
        if (ObjectUtils.isEmpty(height)) {
            ToastUtils.showLongToast(context, "请输入身高");
            return;
        }
        if (ObjectUtils.isEmpty(receivingAddress)) {
            ToastUtils.showLongToast(context, "请输入收货地址");
            return;
        }
        mPresent.activation(realName, identityNum, email, weight, height, receivingAddress, userId);

    }

    //激活成功弹窗
    AlertDialog successDialog = null;

    public void successTipDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_success)
                .setWidthAndHeight(SizeUtils.dp2px(context, 100), SizeUtils.dp2px(context, 107))
                .setText(R.id.tx_tip, "激活成功");
        successDialog = builder.create();
        successDialog.show();
    }

    @Override
    public void getResultDate(Response<ResultDTO> resultDTOResponse) {
        String code = resultDTOResponse.body().getCode();
        String msg = resultDTOResponse.body().getMsg();
        Object data = resultDTOResponse.body().getData();
        if (code.equals("200")) {
            Gson gs = new Gson();
            StaticValue.user = gs.fromJson(gs.toJson(data), User.class);//保存用户信息
            successTipDialog();
//            ToastUtils.showLongToast(context, "激活成功");

            BusFactory.getBus().post(new EventImpl.ActiveAccountEvent());
            SPUtils.putString(Constant.RECEIVE_ADDRESS, StaticValue.user.getReceivingAddress(), context);
            SPUtils.putString(Constant.RECEIVE_NAME,StaticValue.user.getReceivingName(),context);
            SPUtils.putString(Constant.RECEIVE_PHONE,StaticValue.user.getReceivingPhone(),context);
            StaticValue.userAssets.setSpeed(1);
            finish();
            return;
        }
        ToastUtils.showLongToast(context, msg);
    }

    @Override
    protected ActivationUserContact.ActivationUserPresent createPresent() {
        return new ActivationUserContact.ActivationUserPresent();
    }


    @OnClick(R.id.return_btn)
    public void onViewClicked() {
        finish();
    }
}
